package com.wteam.dragon.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.model.mapper.RolesDeptsMapper;
import com.wteam.dragon.model.pojo.bean.RolesDepts;
import com.wteam.dragon.model.service.RolesDeptsService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


/**
 * @Author 陈晓辉
 * @Date 2020/4/2 9:38
 */
@Service
@RequiredArgsConstructor
public class RolesDeptsServiceImpl implements RolesDeptsService {
    private final RolesDeptsMapper rolesDeptsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editRolesDepts(Long roleId, Set<Long> deptIds) {
        QueryWrapper<RolesDepts> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RolesDepts::getRoleId, roleId);
        rolesDeptsMapper.delete(queryWrapper);
        for (Long deptId : deptIds){
            RolesDepts rolesDepts = new RolesDepts();
            rolesDepts.setRoleId(roleId);
            rolesDepts.setDeptId(deptId);
            rolesDeptsMapper.insert(rolesDepts);
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更新成功");
        return resultMessage;
    }

}
